import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'package:flutter/services.dart';

import 'StripeController.dart';
import 'api.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final StripeController stripeController = Get.put(StripeController());

  @override
  void initState() {
    super.initState();
  }
  final TextEditingController _controller = TextEditingController(text: "0.5");
  final RegExp _numericRegExp = RegExp(r'^\d+\.?\d*$');
  String amountInput = "0.5";
  String paymentsValueId = "";
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: SingleChildScrollView(
          child: Column(
              children: [
                Obx(() => Text(stripeController.batteryLevel.value.toString())),
                // Center(
                //   child: ElevatedButton(
                //     onPressed: () async {
                //       await stripeController.stripeTerminalTapToPay.permissionAllowed();
                //     },
                //     child: const Text("PermissionAllowed"),
                //   ),
                // ),
                Center(
                  child: ElevatedButton(
                    onPressed: () async {
                      await stripeController.init("tml_E0yYiA2ECKaz0s", "http://192.168.85.101:8080/catering-api/sys/connectionToken");
                    },
                    child: const Text("Init"),
                  ),
                ),
                Obx(() => Text(stripeController.onInitMsg.value)),
                Obx(() => Text(stripeController.connectReadersStatusMsg.value)),

                Center(
                  child: TextField(
                    controller: _controller,
                    decoration: const InputDecoration(
                      hintText: 'Amount',
                    ),
                    keyboardType: const TextInputType.numberWithOptions(decimal: true),
                    inputFormatters: [
                      FilteringTextInputFormatter.allow(_numericRegExp),
                    ],
                    onChanged: (value){
                      setState(() {
                        amountInput = value;
                      });
                    },
                  ),
                ),
                Center(
                  child: ElevatedButton(
                    onPressed: () async {
                      setState(() {
                        stripeController.onPayMessage.value = "";
                      });
                      Api().posCreatePaymentIntent(amountInput).then((value) {
                        stripeController.stripeTerminalUsb.pay(value).then((paymentsValue){
                          print(paymentsValue);
                          if (paymentsValue['status'] == 'REQUIRES_CAPTURE') {
                            // Api().capturePaymentIntent(
                            //     paymentsValue["id"])
                            //     .then((value) async {
                            // });
                          }
                        });
                      });
                    },
                    child: Text("支付: \$ $amountInput"),
                  ),
                ),
                Obx(() => Text(stripeController.onPayMessage.value)),

                Center(
                  child: ElevatedButton(
                    onPressed: () async {
                      stripeController.stripeTerminalUsb.processPayment().then((paymentsValue){
                        print(paymentsValue);
                        if (paymentsValue['status'] == 'REQUIRES_CAPTURE') {
                          paymentsValueId  = paymentsValue["id"];
                          print("----------------确认----------------------");
                          print(paymentsValueId);
                        }
                      });
                    },
                    child: const Text("确认"),
                  ),
                ),


                Center(
                  child: ElevatedButton(
                    onPressed: () async {
                      Api().tippingIsAddedToTheTransaction(12,paymentsValueId)
                          .then((value) async {
                        print("----------------追加 2----------------------");
                        print(value);
                      });
                    },
                    child: const Text("追加 2"),
                  ),
                ),


                Center(
                  child: ElevatedButton(
                    onPressed: () async {
                      Api().capturePaymentIntent(paymentsValueId)
                          .then((value) async {
                        print("----------------捕获----------------------");
                        print(value);
                      });
                    },
                    child: const Text("捕获"),
                  ),
                ),

                Center(
                  child: ElevatedButton(
                    onPressed: () async {
                      stripeController.stripeTerminalUsb.cancelProcessPayment().then((paymentsValue){
                        print(paymentsValue);
                      });
                    },
                    child: const Text("取消付款意向"),
                  ),
                ),
                Center(
                  child: ElevatedButton(
                    onPressed: () async {
                      stripeController.stripeTerminalUsb.cancelRetrievePaymentIntent().then((value){
                        print(value);
                      });
                    },
                    child: const Text("取消等待刷卡状态"),
                  ),
                ),
                Center(
                  child: ElevatedButton(
                    onPressed: () async {
                      stripeController.stripeTerminalUsb.initOpenCash().then((value)=>{
                        if(value){
                          setState(() {
                            stripeController.onPayMessage.value =
                            "钱箱初始化成功";
                          })
                        }
                      });
                    },
                    child: const Text("初始化连接钱箱"),
                  ),
                ),
                Center(
                  child: ElevatedButton(
                    onPressed: () async {
                      stripeController.stripeTerminalUsb.openCash().then((value)=>{
                        setState(() {
                          stripeController.onPayMessage.value = value["resultCode"] + " " + value["message"];
                        })
                      });
                    },
                    child: const Text("打开钱箱"),
                  ),
                ),
              ]
          ),
        ),
      ),
    );
  }
}
